import nodata from '@/components/nodata/nodata.vue';
import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
export default {
	data() {
		return {
			option: {},
			heights: '156rpx',
			size: 10,
			debug: true,
			// 选择文件后是否立即自动上传
			instantly: true,
			// 微信小程序Map对象for循环不显示，所以转成普通数组，不要问为什么，我也不知道
			count: 8,
			fileList: [],
			detatilId: '',
			Inv: 0,
			// 新增
			num: 1,
			page: false,
			click: '',
			clickN: 0,
			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			click: "",
			totalData: {
				isSubmit: null,
				fId: "", //选中简报的ID
				fProjectid: "", //项目ID
				fProjectname: "", //项目名称
				fCompanyid: "", //公司ID
				fCompanyname: "", //公司名
				fSubunitid: "", //周简报提交单位ID
				fSubunit: "", //周简报提交单位名
				fReportid: "", //填报人ID
				fReportname: "", //填报人名
				fReportingweek: "", //填报周次
				fBriefingoverview: "", //工作简报概述
				fReportdates: "", //填报日期
				fWeeklyreportdates: "", //周简报日期
				fAnnex: "", //资料附件ID
				recordfiles: [], //删除文件id集合
				userId: "", //登录用户id
				userName: "" //登录用户名

			},
			userInfo: {},
			appliance: null,
			timing: null,
			startDates: null
		};
	},
	onReady() {

		this.$nextTick(() => {
			this.$refs.lsjUpload.show();
		})
		var that = this
		console.log(that.totalData.fAnnex)
		if (that.totalData.fAnnex == "" && that.totalData.fAnnex == null && that.totalData.fAnnex ==
			undefined) {
			console.log("文件么准备好")

		} else {
			setTimeout(() => {
				uni.getStorage({
					key: 'userInfo',
					success: function(res) {
						that.userInfo = JSON.parse(res.data)
						var folderid = that.totalData.fAnnex
						var userId = that.userInfo.userId
						var userName = that.userInfo.realName
						console.log(userId)
						var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
							userId + '&userName=' +
							userName;
						that.option = {
							// 上传服务器地址，此地址需要替换为你的接口地址
							url: url,
							// 上传附件的key
							name: 'file',
							header: {},
							formData: {}
						};
					}
				})
			}, 3000)
		}
	},
	onShow() {},
	onLoad(options) {
		this.getLocalData()
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})

		// #endif
		// #ifdef H5
		this.appliance = 1
		// #endif
		var that = this


		var that = this
		uni.getStorage({
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data)

				var data = JSON.parse(res.data)
				if (options.id) {
					that.detatilId = options.id
					that.getDetailData({
						fId: options.id,
						fCompanyid: that.userInfo.companyId
					});
				} else {
					that.getDetailData({
						fId: "",
						fCompanyid: that.userInfo.companyId
					});
				}
			}
		})
		this.getDate()

	},
	components: {
		loading,
		cLabel,
		nodata,
		lsjUpload
	},
	onUnload() {
		uni.$off('drawer-page')
	},
	methods: {
		pdfPage(name, url) {
			let links = encodeURIComponent(url)
			uni.navigateTo({
				url: '/pages/apply/pdf?links=' + links + '&names=' + name,
			})
		},
		// 上传进度回调
		onprogress(item) {
			var that = this
			if (item.responseText) {
				var data = JSON.parse(item.responseText)
				var url = data[0].atta.fFilepath
				var urlPath;
				if (url.indexOf("C:") != -1) {
					urlPath = url.replace(that.$http.redf, that.$http.file)
					that.fileList.push({
						url: urlPath,
						name: item.name,
						fId: data[0].atta.fId
					})
				}
				
				this.$refs.loading.close();
			}
		},
		// 文件选择回调
		onChange(files) {
			this.$refs.loading.open();
			
		},
		getLocalData() {
			var that = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = JSON.parse(res.data)
					// 
					var data = JSON.parse(res.data)
					// this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
					// this.totalData.fWeeklyreportdates = this.common.getDay(0)
					// this.totalData.fReportdates = this.common.getDay(0)
					that.totalData.userId = data.userId; //当前登录人id
					that.totalData.userName = data.realName; //当前登录人名称
					that.totalData.fProjectname = data.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
					that.totalData.fProjectid = data.projectId; //当前登录人项目ID
					that.totalData.fCompanyname = data.companyFullName; //当前登录人标段名称
					that.totalData.fCompanyid = data.companyId; //当前登录人标段ID
					that.totalData.fReportid = data.userId; //填报人id
					that.totalData.fReportname = data.realName; //填报人名称
					that.totalData.fSubunit = data.companyFullName
				}
			})
		},
		clickNum(data) {
			if (this.clickN == 0) {
				this.saveData(data)
			}
		},
		getWeekdates(e) {
			this.totalData.fWeeklyreportdates = e.detail.value
			this.getWeeks(e.detail.value)
		},


		getWeeks(data) {

			var that = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = JSON.parse(res.data)
					that.$http.getWeeks({
						fCompanyid: that.userInfo.companyId,
						startDates: data
					}).then(res => {
						if (res.errcode == 'ok') {
							that.totalData.fReportingweek = res.msg
						}
					})
				}
			})
		},
		getDetailData(data1) {
			var that = this;
			that.$http.getJobbriefingdataByfId(data1).then(res => {
				var data = res.data.yjQmpMsJobbriefingdata;

				if (data1.fId != '') {
					this.totalData = data
					this.fileList = res.data.fAnnexlist
					this.totalData.recordfiles = []
					// let curDate = (new Date()).getTime();
					// let three = this.getDaysBetween(new Date(res.data.startDates), new Date(this.common.getDay(
					// 	0))) * 24 * 3600 * 1000;
					this.startDates = res.data.startDates;
				} else {
					this.totalData.fAnnex = data.fAnnex //资料附件ID
					this.totalData.fReportingweek = data.fReportingweek

					// 			this.totalData.userId = this.userInfo.userId; //当前登录人id
					// 			this.totalData.userName = this.userInfo.realName; //当前登录人名称
					// 			this.totalData.fProjectname = this.userInfo.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
					// 			this.totalData.fProjectid = this.userInfo.projectId; //当前登录人项目ID
					// 			this.totalData.fCompanyname = this.userInfo.companyFullName; //当前登录人标段名称
					// 			this.totalData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
					// 			this.totalData.fReportid = this.userInfo.userId; //填报人id
					// 			this.totalData.fReportname = this.userInfo.realName; //填报人名称
					// this.totalData.fSubunit = this.userInfo.companyFullName

					// 			this.totalData.fSubunitid = this.userInfo.companyId
					this.totalData.recordfiles = []

					this.startDates = res.data.startDates;
					// let curDate = (new Date()).getTime();
					// let three = this.getDaysBetween(new Date(res.data.startDates), new Date(this.common.getDay(
					// 	0))) * 24 * 3600 * 1000;
					// this.startDates = curDate - three;
				}
			});
		},
		getDaysBetween(date1, date2) {
			const ONE_DAY = 1000 * 60 * 60 * 24; // 一天的毫秒数
			const date1Time = date1.getTime(); // 获取时间戳
			const date2Time = date2.getTime();
			const difference = Math.abs(date1Time - date2Time); // 获取时间差
			return Math.round(difference / ONE_DAY); // 两个日期之间的天数
		},
		saveData(data) {

			var that = this
			that.totalData.isSubmit = data.toString()
			if (that.detatilId != "") {
				that.totalData.fId = that.detatilId
			}
			// that.totalData.userId = that.userInfo.userId //当前用户id
			// that.totalData.userName = that.userInfo.realName //当前用户

			if (that.totalData.fBriefingoverview == "" || that.totalData.fBriefingoverview ==
				undefined) {
				uni.showToast({
					title: '工作简报概述不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else {
				that.clickN = 1

				this.$http.saveJobbriefingdata(that.totalData).then(res => {
					// uni.showToast({
					// 	title: res.errcode,
					// 	icon: 'none',
					// 	duration: 10000

					// });
					if (res.errcode == 0) {
						that.$refs.loading.open();



						that.$refs.loading.close();

						that.page = false

						if (!that.click) {
							setTimeout(() => {
								if (data == 0) {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000

									});
								} else if (data == 1) {
									uni.showToast({
										title: "提交成功",
										icon: 'none',
										duration: 2000

									});
								}
								uni.redirectTo({
									url: "/pages/apply/behavior/workReportWeek/testing/index?Inv=" +
										data,
									success: () => {
										that.clickN = 1
										that.click = false
										that.$refs.loading.close();
										// #ifdef H5
										setTimeout(() => {
											that.click = false
										}, 100)

										// #endif

									}
								})
							}, 1500)
						}

						that.click = true

					} else if (res.errcode == -1) {
						uni.showToast({
							title: "该周次已上报，禁止重复上报",
							icon: 'none',
							duration: 2000

						});
						that.clickN = 0
					} else {
						console.log("接口报错")
						that.clickN = 0
					}
				}, (err) => {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				})
			}
		},

		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('workReportWeek')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");
			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;

			this.totalData.fReportdates = timing;
			this.totalData.fWeeklyreportdates = timing;
			this.timing = timing

		},

		// 删除文件
		DelFile(e, id, index) {
			var that = this
			uni.showModal({
				title: '召唤师',
				content: '确定要删除这段回忆吗？',
				cancelText: '再看看',
				confirmText: '再见',
				success: res => {
					if (res.confirm) {

						that.totalData.recordfiles.push({
							fId: id
						})
						this.fileList.splice(index, 1)
						// this.fileList.splice(e.currentTarget.dataset.index, 1)
					}
				}
			})
		},

		callback() {}
	}

};
